using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class Crown : MonoBehaviour
{
    [HideInInspector]
    public ProcTrees2D procTrees;
    public int type;

    // Ellipsoid
    public Vector2 ellipseRadius = new Vector2(5f, 5f);
    public float ellipseDensity = 1f;
    public Vector2 ellipseOffsetX = new Vector2(-1f, 1f);
    public Vector2 ellipseOffsetY = new Vector2(-1f, 1f);

    // Cone


    private int segments = 50;
    private int areaSegments = 50;

    void Ellipse(Vector2 radius)
    {
        float x = 0f;
        float y = 0f;
        float xt;
        float yt;
        float z = 0f;

        float angle = 20f;

        for (int i = 0; i < (segments + 1); i++)
        {
            xt = x;
            yt = y;
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * radius.x;
            y = Mathf.Cos(Mathf.Deg2Rad * angle) * radius.y;

            if(i > 0)
            {
                Gizmos.DrawLine(new Vector3(xt, yt, z) + transform.position, new Vector3(x, y, z) + transform.position);
            }

            angle += (360f / segments);
        }
    }

    /*void EllipseArea()
    {
        for (int i = -areaSegments; i < areaSegments; i++)
        {
            float factor = (float)i / (float)areaSegments;
            Gizmos.DrawLine(new Vector3(transform.position.x + ellipseRadius.x * factor, transform.position.y + Mathf.Sqrt(Mathf.Pow(ellipseRadius.y, 2) * (1 - (Mathf.Pow(ellipseRadius.x * factor, 2) / Mathf.Pow(ellipseRadius.x, 2)))), 0f), new Vector3(transform.position.x + ellipseRadius.x * factor, transform.position.y - Mathf.Sqrt(Mathf.Pow(ellipseRadius.y, 2) * (1 - (Mathf.Pow(ellipseRadius.x * factor, 2) / Mathf.Pow(ellipseRadius.x, 2)))), 0f));
        }
    }*/

    void OnDrawGizmos()
    {
        Gizmos.DrawIcon(transform.position, "crown.png", true);

        if (type == 0)
        {
            Gizmos.color = Color.green;
            Ellipse(ellipseRadius);
            Gizmos.color = Color.red;
            Ellipse(ellipseRadius * (1f-ellipseDensity));
            Gizmos.DrawLine(new Vector3(transform.position.x + ellipseRadius.x * ellipseOffsetX.x, transform.position.y + Mathf.Sqrt(Mathf.Pow(ellipseRadius.y, 2) * (1 - (Mathf.Pow(ellipseRadius.x * ellipseOffsetX.x, 2) / Mathf.Pow(ellipseRadius.x, 2)))), 0f), new Vector3(transform.position.x + ellipseRadius.x * ellipseOffsetX.x, transform.position.y - Mathf.Sqrt(Mathf.Pow(ellipseRadius.y, 2) * (1 - (Mathf.Pow(ellipseRadius.x * ellipseOffsetX.x, 2) / Mathf.Pow(ellipseRadius.x, 2)))), 0f));
            Gizmos.DrawLine(new Vector3(transform.position.x + ellipseRadius.x * ellipseOffsetX.y, transform.position.y + Mathf.Sqrt(Mathf.Pow(ellipseRadius.y, 2) * (1 - (Mathf.Pow(ellipseRadius.x * ellipseOffsetX.y, 2) / Mathf.Pow(ellipseRadius.x, 2)))), 0f), new Vector3(transform.position.x + ellipseRadius.x * ellipseOffsetX.y, transform.position.y - Mathf.Sqrt(Mathf.Pow(ellipseRadius.y, 2) * (1 - (Mathf.Pow(ellipseRadius.x * ellipseOffsetX.y, 2) / Mathf.Pow(ellipseRadius.x, 2)))), 0f));
            Gizmos.DrawLine(new Vector3(transform.position.x + Mathf.Sqrt(Mathf.Pow(ellipseRadius.x, 2) * (1 - (Mathf.Pow(ellipseRadius.y * ellipseOffsetY.x, 2) / Mathf.Pow(ellipseRadius.y, 2)))), transform.position.y + ellipseRadius.y * ellipseOffsetY.x, 0f), new Vector3(transform.position.x - Mathf.Sqrt(Mathf.Pow(ellipseRadius.x, 2) * (1 - (Mathf.Pow(ellipseRadius.y * ellipseOffsetY.x, 2) / Mathf.Pow(ellipseRadius.y, 2)))), transform.position.y + ellipseRadius.y * ellipseOffsetY.x, 0f));
            Gizmos.DrawLine(new Vector3(transform.position.x + Mathf.Sqrt(Mathf.Pow(ellipseRadius.x, 2) * (1 - (Mathf.Pow(ellipseRadius.y * ellipseOffsetY.y, 2) / Mathf.Pow(ellipseRadius.y, 2)))), transform.position.y + ellipseRadius.y * ellipseOffsetY.y, 0f), new Vector3(transform.position.x - Mathf.Sqrt(Mathf.Pow(ellipseRadius.x, 2) * (1 - (Mathf.Pow(ellipseRadius.y * ellipseOffsetY.y, 2) / Mathf.Pow(ellipseRadius.y, 2)))), transform.position.y + ellipseRadius.y * ellipseOffsetY.y, 0f));
            // Area
            /*Gizmos.color = new Color(0.117f, 0.314f, 0f);
            EllipseArea();*/
        }
    }

    void OnDisable()
    {
        // Clean up ProcTrees2D list
        procTrees.crowns.Remove(gameObject);
    }
}
